package com.cqndt.disaster.device.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.cqndt.disaster.device.util.AjaxResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cqndt.disaster.device.service.AppTabProjectService;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author lgb
 * @date 2020-9-1
 * @return 项目管理
 **/
@RestController
@RequestMapping(value = "/api/v2/project")
public class ProjectInfoController extends BaseController {

	@Resource
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private AppTabProjectService tabProjectService;

    /**
     * @param request
     * @param search 查询条件(项目名称或项目地址)
     * @param id 灾害类型
     * @return 查询监测项目列表
     **/
    @PostMapping(value = "/selectAll")
    public AjaxResult selectProjectAll(HttpServletRequest request, String search, String id){
    		Object data=null;
    		try {
    			String userId = request.getHeader("id");
				String areaCode= stringRedisTemplate.opsForValue().get("areaCode"+userId);
				String unitLevel=stringRedisTemplate.opsForValue().get("level"+userId);
				data = tabProjectService.selectByCondition(userId,unitLevel,areaCode,id,search);
			} catch (Exception e) {
				e.printStackTrace();
			}
    	return entityResult(data,"获取项目列表");
    }
    /**
     * @param request
     * @return 查询监测项目置顶的
     **/
    @PostMapping(value = "/selectAllPush")
    public AjaxResult selectProjectAllPush(HttpServletRequest request){
    	Object data = null;
		try {
				String userId = request.getHeader("id");
				data = tabProjectService.selectAllPush(userId);
			} catch (Exception e) {
				System.out.println("监测项目置顶查询失败！");
				e.printStackTrace();
    	}
    	return entityResult(data,"获取项目置顶列表");
    }
 
    /**
     * @param request
     * @param projectId 项目id
     * @return 查询监测项目详情
     **/
    @PostMapping(value = "/selectByIdDetails")
    public AjaxResult selectByIdDetails(HttpServletRequest request,Integer projectId){
    		Object data = null;
			try {
				String userId = request.getHeader("id");
				data = tabProjectService.selectByIdDetails(projectId,userId);
			} catch (Exception e) {
				System.out.println("项目详情查询失败！");
				e.printStackTrace();
			}
    	return entityResult(data,"项目详情查询");
    }

	/**
	 * @param request
	 * @param file 文件流
	 * @param projectId 项目id
	 * @return 上传项目图片
	 **/
	@PostMapping(value="/uploadProjectImg")
	public AjaxResult uploadProjectImg(HttpServletRequest request, 
			MultipartFile file, Integer projectId){
		Object data = null;
    	try {
			String userId = request.getHeader("id");
			int imgs = tabProjectService.uploadProjectImg(file, projectId, userId);
			if(imgs == -1){
				data = 0;
			} else {
				data = true;
			}
		}catch (Exception e){
			System.out.println("项目上传图片失败！");
			e.printStackTrace();
		}
		return entityResult(data,"项目上传图片");
	}

	/**
	 * @param request
	 * @param file 文件流
	 * @param projectId 项目id
	 * @return 项目上传视频
	 **/
	@PostMapping(value="/uploadProjectVideo")
	public AjaxResult uploadProjectVideo(HttpServletRequest request, MultipartFile file, Integer projectId){
		Object data = null;
		try {
			String userId = request.getHeader("id");
			int imgs = tabProjectService.uploadProjectVideo(file, projectId, userId);
			if(imgs == -1){
				data = 0;
			} else {
				data = true;
			}
		}catch (Exception e){
			System.out.println("项目上传视频失败！");
			e.printStackTrace();
		}
		return entityResult(data,"项目上传视频");
	}

	/**
	 * @param request
	 * @param fileId 文件流
	 * @param projectId 项目id
	 * @param type
	 * @return 项目删除图片或视频
	 **/
	@PostMapping(value = "/deleteProjectOrVideo")
	public AjaxResult deleteProjectOrVideo(HttpServletRequest request,
			Integer fileId,Integer projectId,Integer type) {
		Object data = null;
		try {
			String userId = request.getHeader("id");
			int delete = tabProjectService.deleteProjectOrVideo(fileId, projectId, userId, type);
			if(delete == -1){
				data = 0;
			} else {
				data = true;
			}
		}catch (Exception e){
			System.out.println("删除失败！");
			e.printStackTrace();
		}
		return entityResult(data,"删除");
	}
}